package ui
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class Component extends Sprite
	{
		protected var _enabled : Boolean;
		protected var _width : int;
		protected var _height : int;
				
		protected var _backGroundContent : DisplayObject;		
		
		/**
		 * 四周的余角,默认为3 
		 */		
		protected var _corner : int = 3;	
		protected var _fixCustomBG:Boolean = true;	
		protected var _backGroundChanged : Boolean = false;
		protected var _backGround : Boolean;
		protected var _backGroundColor : uint;
		protected var _backGroundAlpha : Number;
		
		/**
		 * 是否显示边框 
		 */		
		protected var _border : Boolean = false;	
		protected var _borderThickness : Number;
		protected var _borderColor : uint;
		protected var _borderAlpha : uint;
		
		private var mouseDownX : int;
		private var mouseDownY : int;
		private var _maskShape : Shape;
		
		public function Component()
		{
			super();
			
			init();
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
		}
		
		private function mouseDownHandler(e : MouseEvent) : void
		{
			mouseDownX = e.stageX;
			mouseDownY = e.stageY;
			this.addEventListener(MouseEvent.MOUSE_MOVE , _mouseMove);
			this.addEventListener(MouseEvent.MOUSE_UP , _stopMoveHandler);
		}
		
		private function _mouseMove(e : MouseEvent):void
		{
			var dragStartX : int = e.stageX;
			var dragStartY : int = e.stageY;
		}
		
		private function _stopMoveHandler(e : MouseEvent):void
		{
			this.removeEventListener(MouseEvent.MOUSE_MOVE , _mouseMove);
			this.removeEventListener(MouseEvent.ROLL_OUT , _stopMoveHandler);
		}
		
		/**
		 * 数据初始化
		 */
		protected function init () : void
		{
			createChildren();
			invalidate();
		}
		
		/**
		 * 构建子对象
		 */
		protected function createChildren () : void
		{
		}
		
		/**
		 * 使失效，将在下一帧中更新
		 *
		 */
		public function invalidate () : void
		{
			addEventListener(Event.ENTER_FRAME, onEnterFrameHandler , false ,0 , true);
		}
		
		protected function onEnterFrameHandler (evt : Event) : void
		{
			removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
			validate();
		}
		
		/**
		 * 使生效，刷新屏幕
		 */
		public function validate () : void
		{
			commitProperty();
			measure();
			updateDisplayList();
			layoutChrom();
		}
		
		/**
		 * 提交属性修改
		 */
		protected function commitProperty () : void
		{
			
		}
		
		/**
		 * 重新估量数据
		 */
		protected function measure () : void
		{
			
		}
		
		/**
		 * 排版，布局
		 */
		protected function layoutChrom () : void
		{
			
		}
		
		/**
		 * 重绘
		 */
		public function updateDisplayList () : void
		{
			drawBackGround();
			if (_backGroundContent)
			{
				if(super.contains(_backGroundContent))
					super.setChildIndex(_backGroundContent, 0);
				else
					trace('backGroundSkin 已经被从显示列表中移除了');
				_backGroundContent.x = borderThickness;
				_backGroundContent.y = borderThickness;
				if(_fixCustomBG){
					_backGroundContent.width = _width - borderThickness * 2;
					_backGroundContent.height = _height - borderThickness * 2;
				}
				else
				{
					_backGroundContent.width = _width;
					_backGroundContent.height = _height;
				}
				if(corner > 0)
				{
					if(!_maskShape)
					{
						_maskShape = new Shape();
						addChild(_maskShape);
					}
					_backGroundContent.mask = _maskShape;
					
					_maskShape.graphics.clear();
					_maskShape.graphics.beginFill(0x00000000 , 1);
					_maskShape.graphics.drawRoundRect(0 , 0 , _width , _height, corner , corner);
					_maskShape.graphics.endFill();
				}
			}
			else
			{
				if(_maskShape && contains(_maskShape))
				{
					removeChild(_maskShape);
					_maskShape = null;	
				}
			}
		}
		
		protected function drawBackGround () : void
		{
			if(!_backGroundChanged)return;
			graphics.clear();
			if (_backGround)
			{
				graphics.beginFill(_backGroundColor, _backGroundAlpha);
				graphics.drawRoundRect(0, 0, _width, _height, _corner, _corner);
				graphics.endFill();
			}
			if (border)
			{
				graphics.lineStyle(_borderThickness, _borderColor, _borderAlpha);
				graphics.drawRoundRect(0, 0, _width, _height, _corner, _corner);
				graphics.endFill();
			}
			_backGroundChanged = false;
		}
		
		/**
		 * 是否要绘制背景，要更改颜色，请设置backGroundColor
		 * @param value
		 */
		public function get backGround () : Boolean
		{
			return _backGround;
		}
		
		public function set backGround (value : Boolean) : void
		{
			if(_backGround == value)
				return;
			_backGround = value;
			_backGroundChanged = true;
			callLaterDrawBackGround();
		}
		
		/**
		 * 背景颜色
		 * @return
		 */
		public function get backGroundColor () : uint
		{
			return _backGroundColor;
		}
		
		public function set backGroundColor (value : uint) : void
		{
			if(_backGroundColor == value)
				return;
			_backGroundColor = value;
			_backGroundChanged = true;
			callLaterDrawBackGround();
		}
		
		/**
		 * 背景alpha值
		 * @return
		 *
		 */
		public function get backGroundAlpha () : Number
		{
			return _backGroundAlpha;
		}
		
		public function set backGroundAlpha (value : Number) : void
		{
			if(_backGroundAlpha == value)
				return;
			_backGroundAlpha = value;
			_backGroundChanged = true;
			callLaterDrawBackGround();
		}
		
		/**
		 * 四周的余角,默认为3 , 设置该值会调用invalidate使用界面失效
		 * @return
		 */
		public function get corner () : int
		{
			return _corner;
		}
		
		public function set corner (value : int) : void
		{
			if(_corner == value)
				return;
			_corner = value;
			_backGroundChanged = true;
			invalidate();
		}
		
		/**
		 * 是否显示边框, 设置该值会调用invalidate使用界面失效
		 * @return
		 *
		 */
		public function get border () : Boolean
		{
			return _border;
		}
		
		public function set border (value : Boolean) : void
		{
			if(_border == value)
				return;
			_border = value;
			_backGroundChanged = true;
			invalidate();
		}
		
		/**
		 * 边框精细, 设置该值会调用invalidate使用界面失效
		 * @return
		 *
		 */
		public function get borderThickness () : Number
		{
			return _borderThickness;
		}
		
		public function set borderThickness (value : Number) : void
		{
			if(_borderThickness == value)
				return;
			_borderThickness = value;
			_backGroundChanged = true;
			invalidate();
		}
		
		/**
		 * 边框颜色
		 * @return
		 */
		public function get borderColor () : uint
		{
			return _borderColor;
		}
		
		public function set borderColor (value : uint) : void
		{
			if(_borderColor == value)
				return;
			_borderColor = value;
			_backGroundChanged = true;
			callLaterDrawBackGround();
		}
		
		/**
		 * 边框透明度
		 * @return
		 */
		public function get borderAlpha () : uint
		{
			return _borderAlpha;
		}
		
		public function set borderAlpha (value : uint) : void
		{
			if(_borderAlpha == value)
				return;
			_borderAlpha = value;
			_backGroundChanged = true;
			callLaterDrawBackGround();
		}
		
		private function callLaterDrawBackGround():void
		{
			super.addEventListener(Event.ENTER_FRAME , onLaterDrawBackGround , false , 0 ,true);
		}
		
		private function onLaterDrawBackGround(e : Event):void
		{
			super.removeEventListener(Event.ENTER_FRAME , onLaterDrawBackGround);
			drawBackGround();
		}
		
		override public function get width () : Number
		{
			return _width;
		}
		
		/**
		 * 设置该值会调用invalidate使用界面失效
		 * @param value
		 */		
		override public function set width (value : Number) : void
		{
			value = Math.round(value);
			if (_width != value)
			{
				_width = value;
				_backGroundChanged = true;
				invalidate();
				dispatchEvent(new Event(Event.RESIZE));
			}
		}
		
		/**
		 * 设置该值会调用invalidate使用界面失效
		 * @param value
		 */		
		override public function set height (value : Number) : void
		{
			value = Math.round(value);
			if (_height != value)
			{
				_height = value;
				_backGroundChanged = true;
				invalidate();
				dispatchEvent(new Event(Event.RESIZE));
			}
		}
		
		override public function get height () : Number
		{
			return _height;
		}
		
		public function set enabled (value : Boolean) : void
		{
			_enabled = value;
			mouseEnabled = _enabled;
			mouseChildren = _enabled;
			tabEnabled = value;
			//			alpha = _enabled ? 1.0 : 0.5;
		}
		
		public function get enabled () : Boolean
		{
			return _enabled;
		}
		
		/**
		 * 移动位置
		 * @param xpos
		 * @param ypos
		 */
		public function move (xpos : Number, ypos : Number) : void
		{
			this.x = int(xpos);
			this.y = int(ypos);
		}
		
		/**
		 * 设置大小, 设置该值会调用invalidate使用界面失效
		 * @param w
		 * @param h
		 */
		public function setSize (w : int, h : int) : void
		{
			if (_width == w && _height == h)
				return;
			_width = w;
			_height = h;
			_backGroundChanged = true;
			invalidate();
		}
		
		/**
		 * 清除内存
		 */
		public function clear () : void
		{
			if (hasEventListener(Event.ENTER_FRAME))
				removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
//			_data = null;
		}
		
		
	}
}